<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author:kane < chengjin005@163.com>
// +----------------------------------------------------------------------
namespace app\lose\controller;

use cmf\controller\AdminBaseController;
use app\lose\model\WechatPublicNumberModel;
use think\Db;

/**
 * Class AdminTagController 标签管理控制器
 * @package app\lose\controller
 */
class AdminWechatPublicNumberController extends AdminBaseController
{
    /**
     * 公众号管理
     * @adminMenu(
     *     'name'   => '公众号',
     *     'parent' => 'lose/AdminIndex/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '公众号',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        $data = db('wechat_public_number')->paginate();

        // $this->assign("arrStatus", $loseTagModel::$STATUS);

        // $this->assign("tags", $tags);
        $this->assign('list', $data->items());
        $this->assign('page', $data->render());

        $actions = ['add', 'edit', 'delete'];
        $this->assign('actions', actions_check($this->request->module().'/'.$this->request->controller().'/', $actions));

        return $this->fetch();
    }

    /**
     * 添加公众号
     * @adminMenu(
     *     'name'   => '添加公众号',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加公众号',
     *     'param'  => ''
     * )
     */
    public function add()
    {
        return $this->fetch();
    }

    /**
     * 添加公众号提交
     * @adminMenu(
     *     'name'   => '添加公众号提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加公众号提交',
     *     'param'  => ''
     * )
     */
    public function addPost()
    {

        $data   = $this->request->param();

        $wechatPublicNumber = new WechatPublicNumberModel();
        $wechatPublicNumber->adminAddPublicNumber($data);

        //操作日志
        admin_log($data['name'], 'add', 'public_number');

        $this->success(lang("ADD_SUCCESS"));

    }

    /**
     * 编辑公众号
     * @adminMenu(
     *     'name'   => '编辑公众号',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑公众号',
     *     'param'  => ''
     * )
     */
    public function edit(){
        $number_id = $this->request->param('number_id', 0, 'intval');

        $wechatPublicNumber = new WechatPublicNumberModel();
        $data               = $wechatPublicNumber->where('number_id', $number_id)->find();

        $this->assign("data",$data);

        return $this->fetch();
    }

    /**
     * 编辑公众号提交
     * @adminMenu(
     *     'name'   => '编辑公众号提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑公众号提交',
     *     'param'  => ''
     * )
     */
    public function editPost(){
        if ($this->request->isPost()) {
            $data   = $this->request->param();

            $wechatPublicNumber = new WechatPublicNumberModel();
            $wechatPublicNumber->adminEditPublicNumber($data);
            
            //操作日志
            admin_log($data['name'], 'edit', 'public_number');

            
            $this->success(lang("SAVE_SUCCESS"));
        }
    }

    /**
     * 删除公众号
     * @adminMenu(
     *     'name'   => '删除公众号',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '删除公众号',
     *     'param'  => ''
     * )
     */
    public function delete()
    {
        $number_id = $this->request->param("number_id", 0, 'intval');

        if (empty($number_id)) {
            $this->error(lang("NO_ID"));
        }

        //删除公众号
        $wechatPublicNumber = new WechatPublicNumberModel();
        $wechatPublicNumber->where(['number_id' => $number_id])->delete();
            
        //若已发布的文章，仅在该公众号下发布，则把文章删除
        

        $this->success(lang("DELETE_SUCCESS"));
    }
}
